home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / p-interp.lha / p-interp-0.5 / turtlegr.c < prev    next >
C/C++ Source or Header  |  2002-06-24  |  9KB  |  408 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  
  21.   $Log: turtlegr.c,v $
  22.   Revision 1.4  2001/06/06 23:14:19  mario
  23.   Turtlegraphics wird jetzt mit einem #define aktiviert
  24.  
  25.   Revision 1.3  2001/05/23 21:16:41  mario
  26.   Turtlegraphics wurde als eigener Prozess ausgelagert.
  27.  
  28.   Revision 1.2  2001/05/20 13:12:02  mario
  29.   CVS-Idents und Logs eingefügt
  30.  
  31.  
  32. */
  33.  
  34. #ifdef TURTLEGRAPHICS
  35.  
  36. #ident "$Id: turtlegr.c,v 1.4 2001/06/06 23:14:19 mario Exp $";
  37.  
  38. #include <math.h>
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. #include <stdarg.h>
  42. #include <sys/types.h>
  43. #ifndef _AMIGA
  44. #include <sys/socket.h>
  45. #else
  46. #include <stdlib.h>
  47. #include <string.h>
  48.  
  49. #define EXTERN
  50. #include "rexx.h"
  51. #define rint(f) floor(f+.5)
  52. #endif
  53. #include <sys/wait.h>
  54.  
  55. #include "psystem.h"
  56. #include "pcode.h"
  57. #include "Memory.h"
  58. #include "Stack.h"
  59.  
  60. #ifndef _AMIGA
  61. static FILE *TurtleServer=NULL;
  62. #else
  63. static TurtleServer=0;
  64. #endif
  65.  
  66. #define TURTLE_SPEEDUP
  67.  
  68. #ifdef TURTLE_SPEEDUP
  69. static Integer    CurrentTurtleX;
  70. static Integer    CurrentTurtleY;
  71. static Integer    CurrentTurtleAng;
  72. #endif
  73.  
  74. void StartTurtleServer(void)
  75. {
  76. #ifndef _AMIGA
  77.   int fd[2];
  78.   int pid;
  79.   int i;
  80.  
  81.   if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd)<0)
  82.     {
  83.       perror("socketpair()");
  84.       return;
  85.     }
  86.   if ((pid=fork())<0)
  87.     {
  88.       perror("fork()");
  89.       return;
  90.     }
  91.   if (!pid)
  92.     {
  93.       if (fd[1]!=0)
  94.     {
  95.       close(0);
  96.       dup2(fd[1],0);
  97.     }
  98.       if (fd[1]!=1)
  99.     {
  100.       close(1);
  101.       dup2(fd[1],1);
  102.     }
  103.       for (i=getdtablesize();i>2;i--)
  104.     close(i);
  105.       execlp("xturtleserver", "xturtleserver", NULL);
  106.       execlp("./xturtleserver", "xturtleserver", NULL);
  107.       perror("xturtleserver");
  108.       _exit(1);
  109.     }
  110.   close(fd[1]);
  111.   TurtleServer=fdopen(fd[0], "wb+");
  112. #else
  113.    struct RexxMsg *rexxmessage;   /* incoming rexx messages */
  114.    char *buff = "QUI";     /* Test message for TurtleServer */
  115.  
  116.    TurtleServer = !system("run >con: arexxturtleserver");
  117.    /* set up a public port for rexx to talk to us later */
  118.    if (NULL == (rexx_port = setup_rexx_port()))
  119.    {
  120.      printf("sorry, couldn't set up our public rexx port\n");
  121.      close_up_shop(10);
  122.    }
  123.    /* Send a test command */
  124.    if (send_rexx_command(buff) == OK)
  125.    {
  126.       /* now wait for something to come from the user or from rexx */
  127.       Wait(1L<<rexx_port->mp_SigBit);
  128.       /* got something!! */
  129.       /* It has to be a reply from rexx, because we don't do anything else
  130. */
  131.       while(rexxmessage = (struct RexxMsg *)GetMsg(rexx_port))
  132.       {
  133.          /* is this a reply to a previous message? */
  134.          if (rexxmessage->rm_Node.mn_Node.ln_Type == NT_REPLYMSG)
  135.          {
  136.             printf("the command '%s' has terminated with code %ld, %ld\n",
  137. rexxmessage->rm_Args[0],rexxmessage->rm_Result1,rexxmessage->rm_Result2);
  138.             free_rexx_command(rexxmessage);
  139.          }
  140.       }
  141.    }
  142.    /* The command failed.. */
  143.    else
  144.       puts(NO_REXX_MSG);
  145. }
  146.  
  147. void close_up_shop(int value)
  148. {
  149.    if (rexx_port)
  150.      shutdown_rexx_port(rexx_port);
  151.    exit(value);
  152. #endif
  153. }
  154.  
  155. void tprintf(char *format, ...)
  156. {
  157.   va_list ap;
  158. #ifdef _AMIGA
  159.   char str[64];
  160. #endif
  161.   if (!TurtleServer)
  162.     StartTurtleServer();
  163.  
  164.   if (TurtleServer)
  165.     {
  166.       va_start(ap, format);
  167. #ifndef _AMIGA
  168.       vfprintf(TurtleServer, format, ap);
  169. #else
  170.       vsprintf(str, format, ap);
  171. #endif
  172.       va_end(ap);
  173. #ifndef _AMIGA
  174.       putc('\n', TurtleServer);
  175. #else
  176.    /* Send a test command */
  177.    if (send_rexx_command(str) == OK)
  178.    {
  179.       struct RexxMsg *rexxmessage;   /* incoming rexx messages */
  180.       /* now wait for something to come from the user or from rexx */
  181.       Wait(1L<<rexx_port->mp_SigBit);
  182.       /* got something!! */
  183.       /* It has to be a reply from rexx, because we don't do anything else
  184. */
  185.       while(rexxmessage = (struct RexxMsg *)GetMsg(rexx_port))
  186.       {
  187.          /* is this a reply to a previous message? */
  188.          if (rexxmessage->rm_Node.mn_Node.ln_Type == NT_REPLYMSG)
  189.          {
  190.             printf("the command '%s' has terminated with code %ld, %ld\n",
  191. rexxmessage->rm_Args[0],rexxmessage->rm_Result1,rexxmessage->rm_Result2);
  192.             free_rexx_command(rexxmessage);
  193.          }
  194.       }
  195.    }
  196.    /* The command failed.. */
  197.    else
  198.       puts(NO_REXX_MSG);
  199. #endif
  200.     }
  201. }
  202.  
  203. Integer tgetint(void)
  204. {
  205.   int i=0;
  206. #ifndef _AMIGA
  207.   char    Buffer[20];
  208.  
  209.   fflush(TurtleServer);
  210.  
  211.   for (i=0; i<19;i++)
  212.     {
  213.       Buffer[i]=getc(TurtleServer);
  214.       if (Buffer[i]=='\n')
  215.     break;
  216.     }
  217.  
  218.   Buffer[i]='\0';
  219.  
  220.   sscanf(Buffer, "%d", &i);
  221. #endif
  222.   return ((Integer)i);
  223. }
  224.  
  225. #ifdef TURTLE_SPEEDUP
  226. void TurnTo(int Angle)
  227. {
  228.   CurrentTurtleAng=Angle;
  229.   while (CurrentTurtleAng<0)
  230.     CurrentTurtleAng+=360;
  231.   while (CurrentTurtleAng>=360)
  232.     CurrentTurtleAng-=360;
  233. }
  234. #endif
  235.  
  236. void TurtleGraphics(word EntryPoint)
  237. {
  238.   int    i;
  239.   char    func[64];
  240.   char    *p;
  241.  
  242.   i=0;
  243.   for (i=0; i<sizeof(func); i++)
  244.     if (!(func[i]=MemRdByte(EntryPoint,i)))
  245.       break;
  246.  
  247.   p=&func[16];
  248.   if (strcmp(p, "INITTURTLE")==0)
  249.     tprintf("INITTURTLE");
  250.   else if (strcmp(p, "TURN")==0)
  251.     {
  252.       Integer i=PopInteger();
  253.       tprintf("TURN %d",i);
  254. #ifdef TURTLE_SPEEDUP
  255.       TurnTo(CurrentTurtleAng+i);
  256. #endif
  257.     }
  258.   else if (strcmp(p, "TURNTO")==0)
  259.     {
  260.       Integer i=PopInteger();
  261.       tprintf("TURNTO %d",i);
  262. #ifdef TURTLE_SPEEDUP
  263.       TurnTo(i);
  264. #endif
  265.     }
  266.   else if (strcmp(p, "MOVE")==0)
  267.     {
  268.       Integer i=PopInteger();
  269.       tprintf("MOVE %d",i);
  270. #ifdef TURTLE_SPEEDUP
  271.       CurrentTurtleX+=rint(cos(CurrentTurtleAng*3.14/180)*i);
  272.       CurrentTurtleY+=rint(sin(CurrentTurtleAng*3.14/180)*i);
  273. #endif
  274.     }
  275.   else if (strcmp(p, "MOVETO")==0)
  276.     {
  277.       Integer y=PopInteger();
  278.       Integer x=PopInteger();
  279.       tprintf("MOVETO %d %d",x,y);
  280. #ifdef TURTLE_SPEEDUP
  281.       CurrentTurtleX=x;
  282.       CurrentTurtleY=y;
  283. #endif
  284.     }
  285.   else if (strcmp(p, "PENCOLOR")==0)
  286.     tprintf("PENCOLOR %d",Pop());
  287.   else if (strcmp(p, "TEXTMODE")==0)
  288.     tprintf("TEXTMODE");
  289.   else if (strcmp(p, "GRAFMODE")==0)
  290.     tprintf("GRAFMODE");
  291.   else if (strcmp(p, "FILLSCREEN")==0)
  292.     tprintf("FILLSCREEN %d",Pop());
  293.   else if (strcmp(p, "VIEWPORT")==0)
  294.     {
  295.       int yMax=PopInteger();
  296.       int yMin=PopInteger();
  297.       int xMax=PopInteger();
  298.       int xMin=PopInteger();
  299.       tprintf("VIEWPORT %d %d %d %d",xMin, xMax, yMin, yMax);
  300.     }
  301.   else if (strcmp(p, "TURTLEX")==0)
  302.     {
  303.       Pop();
  304.       Pop();
  305. #ifdef TURTLE_SPEEDUP
  306.       Push(CurrentTurtleX);
  307. #else
  308.       tprintf("TURTLEX");
  309.       Push(tgetint());
  310. #endif
  311.     }
  312.   else if (strcmp(p, "TURTLEY")==0)
  313.     {
  314.       Pop();
  315.       Pop();
  316. #ifdef TURTLE_SPEEDUP
  317.       Push(CurrentTurtleY);
  318. #else
  319.       tprintf("TURTLEY");
  320.       Push(tgetint());
  321. #endif
  322.     }
  323.   else if (strcmp(p, "TURTLEANG")==0)
  324.     {
  325.       Pop();
  326.       Pop();
  327. #ifdef TURTLE_SPEEDUP
  328.       Push(CurrentTurtleAng);
  329. #else
  330.       tprintf("TURTLEANG");
  331.       Push(tgetint());
  332. #endif
  333.     }
  334.   else if (strcmp(p, "SCREENBIT")==0)
  335.     {
  336.       Integer y=PopInteger();
  337.       Integer x=PopInteger();
  338.       tprintf("SCREENBIT %d %d",x ,y);
  339.       Push(tgetint());
  340.     }
  341.   else if (strcmp(p, "DRAWBLOCK")==0)
  342.     {
  343.       int    x;
  344.       int    y;
  345.       Integer Mode    = PopInteger();
  346.       Integer YScreen = PopInteger();
  347.       Integer XScreen = PopInteger();
  348.       Integer Height  = PopInteger();
  349.       Integer Width   = PopInteger();
  350.       Integer YSkip   = PopInteger();
  351.       Integer XSkip   = PopInteger();
  352.       Integer RowSize = PopInteger();
  353.       word Source     = Pop();
  354.  
  355.       tprintf("DRAWBLOCK %d %d %d %d %d %d %d %d",
  356.           (((XSkip+Width+7)&~7)-(XSkip&~7))>>3, XSkip&7, 0,
  357.           Width, Height, XScreen, YScreen, Mode);
  358.       for (y=0; y<Height; y++)
  359.     for (x=(XSkip&7);x<((XSkip+Width+7)&~7);x+=8)
  360.       putc(MemRdByte(Source, (y+YSkip)*RowSize+x/8), TurtleServer);
  361.     }
  362.   else if (strcmp(p, "WCHAR")==0)
  363.     {
  364.       tprintf("WCHAR %d",Pop()&0xff);
  365. #ifdef TURTLE_SPEEDUP
  366.       CurrentTurtleX += 7;
  367. #endif
  368.     }
  369.   else if (strcmp(p, "WSTRING")==0)
  370.     {
  371.       word Addr=Pop();
  372.       int  Len=MemRdByte(Addr, 0);
  373.       int  i;
  374.  
  375.       tprintf("WSTRING %d",Len);
  376.       for (i=0;i<Len; i++)
  377.     putc(MemRdByte(Addr, i+1), TurtleServer);
  378. #ifdef TURTLE_SPEEDUP
  379.       CurrentTurtleX += 7*Len;
  380. #endif
  381.     }
  382.   else if (strcmp(p, "CHARTYPE")==0)
  383.     tprintf("WCHAR %d",Pop()&0x0f);
  384.   else
  385.     XeqError(XNOTIMP);
  386. #ifndef _AMIGA
  387.   fflush(TurtleServer);
  388. #endif
  389. }
  390.  
  391. void GraphicsClear(void)
  392. {
  393. #ifndef _AMIGA
  394.   int status;
  395.   if (TurtleServer)
  396.     {
  397.       fclose(TurtleServer);
  398.       TurtleServer=NULL;
  399.       wait(&status);
  400.     }
  401. #else
  402.    /* clean up */
  403. //   close_up_shop(0);
  404. #endif
  405. }
  406.  
  407. #endif /* TURTLEGRAPHICS */
  408.